package ch.usi.inf.pf2.gui;

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.table.TableCellRenderer;

import ch.usi.inf.pf2.cell.Cell;
import ch.usi.inf.pf2.cell.Formula;
import ch.usi.inf.pf2.cell.Text;
import ch.usi.inf.pf2.cell.Value;

/**
 * This class is responsible for telling to the JTable how to draw the cells.
 * @author Thomas Mantegazzi
 *
 */

public class DataRenderer extends JLabel implements TableCellRenderer{

	private Border unselectedBorder = null;
	private Border selectedBorder = null;
	private JTextField text;
	private JTextField cellEditor;
	private JLabel statusLabel;

	/**
	 * Constructor of the class
	 */
	public DataRenderer(JTextField text, JTextField cellEditor, JLabel statusLabel) {
		//TODO (if necessary)
		this.text = text;
		this.cellEditor = cellEditor;
		this.statusLabel = statusLabel;
	}

	/**
	 * Implementation of the abstract method, this says to the table how to draw a given cell.
	 */
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		if(value instanceof String) {
			setText((String)value);
			return this;
		}
		this.setOpaque(true);
		Cell myCell = (Cell)value;
		System.out.println(""+myCell.getColor().toString());
		value = ((Cell)value).getContent();


		if(isSelected) {
			if (selectedBorder == null) {
				selectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
						table.getSelectionBackground());
			}
			setBorder(selectedBorder);
			if(value instanceof Formula) {
				if(((Formula)value).isThereError()) {
					this.setOpaque(true);
					if(((Formula)value).getErrorMessage().equalsIgnoreCase("Missing values in references")) {
						this.setBackground(Color.YELLOW);

					}else {
						this.setBackground(Color.RED);
					}
					setText(((Formula) value).getInstance());
					text.setText(((Formula) value).getInstance());
					cellEditor.setText(((Formula) value).getInstance());
					statusLabel.setText(((Formula) value).getErrorMessage());
					setToolTipText(((Formula) value).getErrorMessage());
					return this;
				}else {
					System.out.println("Sel Form");
					//TODO do something with formula when she is selected
					setText(((Formula) value).getInstance());
					text.setText(((Formula) value).getInstance());
					cellEditor.setText(((Formula) value).getInstance());
					setHorizontalAlignment(JLabel.LEFT);
					setToolTipText("Formula");
					this.setBackground(myCell.getColor());
					statusLabel.setText("    ");
					return this;
				}
			}
			if(value instanceof Value) {
				System.out.println("Sel Value");
				//TODO Do something when A value is selected
				setText(value.toString());
				text.setText(value.toString());
				cellEditor.setText(value.toString());
				setToolTipText("Value");
				setHorizontalAlignment(JLabel.RIGHT);
				this.setBackground(myCell.getColor());
				statusLabel.setText("   ");
				return this;
			}
			if(value instanceof Text) {
				setText(value.toString());
				statusLabel.setText("   ");
				text.setText(value.toString());
				cellEditor.setText(value.toString());
				setHorizontalAlignment(JLabel.LEFT);
				setToolTipText("Text");
				this.setBackground(myCell.getColor());
				return this;
			}
		}else {
			unselectedBorder = myCell.getBorderType();
			setBorder(unselectedBorder);
			if(value instanceof Formula) {
				if(((Formula)value).isThereError()) {
					System.out.println(""+this.getBackground());
					Border border =LineBorder.createGrayLineBorder();
					this.setBorder(border);
					this.setOpaque(true);
					if(((Formula)value).getErrorMessage().equalsIgnoreCase("Missing values in references")) {
						this.setBackground(Color.YELLOW);

					}else {
						this.setBackground(Color.RED);
					}
					setText(((Formula) value).getActualValue());
					setToolTipText(((Formula) value).getErrorMessage());
					return this;
				}else{
					setText(((Formula) value).getActualValue());
					setHorizontalAlignment(JLabel.RIGHT);
					setToolTipText("Formula");
					this.setBackground(myCell.getColor());
					return this;
				}
			}
			if(value instanceof Value) {
				//TODO Do something with Value
				setText(value.toString());
				setHorizontalAlignment(JLabel.RIGHT);
				setToolTipText("Value");
				this.setBackground(myCell.getColor());
				return this;
			}
			if(value instanceof Text) {
				//TODO do something with text
				setText(value.toString());
				setHorizontalAlignment(JLabel.LEFT);
				setToolTipText("Text");
				this.setBackground(myCell.getColor());
				return this;
			}
		}
		if(hasFocus) {
			System.out.println("focus");
		}
		return this;

	}


}
